var S1MValidation = ( function($) {

  var defaultvalidationicon = '<i class=\"fa fa-exclamation-triangle\"></i>';
  var defaulterroricon = '<i class=\"fa fa-exclamation-circle\"></i>';
  var defaultmessageicon = '<i class=\"fa fa-star-o\"></i>';

  function _populateNotifications(clientInfo, notifDivId, validationDivId) {
    if (!notifDivId) {
      notifDivId = IParameterName.WEB_NOTIFICATION_DIV;
    }
    if (!validationDivId) {
      validationDivId = IParameterName.WEB_VALIDATION_DIV;
    }

    if (clientInfo && clientInfo.showValidationBox) {
      var notifDiv = $("#" + notifDivId);
      if (clientInfo.hasErrorMessage) {
        appendErrorDiv(notifDiv, clientInfo.errorClientMessage);
      }

      if (clientInfo.hasInfoMessage) {
        for (var i = 0; i < clientInfo.infoClientMessages.length; i++) {
          appendInfoDiv(notifDiv, clientInfo.infoClientMessages[i]);
        }
      }

      if (clientInfo.hasGeneralValidationMessage) {
        var validDiv = $("#" + validationDivId);
        appendValidationDivs(validDiv, clientInfo.generalValidationClientMessage);

        validDiv = $("#" + IParameterName.WEB_MODAL_VALIDATION_DIV);
        appendValidationDivs(validDiv, clientInfo.generalValidationClientMessage);
      }

      if (clientInfo.hasErrorMessage || clientInfo.hasGeneralValidationMessage || clientInfo.hasInfoMessage) {
        var valDiv = $("#" + validationDivId);
        var errorDiv = $("#" + notifDivId);
        if (clientInfo.hasGeneralValidationMessage || clientInfo.hasInfoMessage) {
          $('html, body').animate({ scrollTop: valDiv.offset().top }, 'slow');
        }
        if (errorDiv) {
          $('html, body').animate({ scrollTop: errorDiv.offset().top }, 'slow');
        }
      }
    }
  }

  function appendErrorDiv(notifDiv, clientMessage) {
    var newDiv = document.createElement('div');
    $(newDiv).addClass("alert alert-error");
    var innerT = '<H3 style="color: inherit">'
                  + clientMessage.icon
                  + ' '
                  + clientMessage.header
                  + '</H3>';
    var messageBody = _populateMessageList(clientMessage.clientMessageDetails);
    $(newDiv).html(innerT + messageBody );
    $(notifDiv).append(newDiv);
  }

  function appendInfoDiv(notifDiv, clientMessage) {
    var infoDiv = document.createElement('div');
    $(infoDiv).addClass("alert alert-success");
    var innerM = '<H3>'
                  + clientMessage.icon
                  + ' '
                  + clientMessage.header
                  + '</H3>';
    var messageBody = _populateMessageList(clientMessage.clientMessageDetails);
    var aref='';
    if (clientMessage.clientMessageDecorator.dismissible) {
      aref='<a style="outline: medium none;" hidefocus="true" class="close" data-dismiss="alert" href="#">&#215;</a>';
    }
    $(infoDiv).html(aref + innerM + messageBody);
    $(notifDiv).append(infoDiv);
  }

  function appendValidationDivs(validDiv, clientMessage) {
    var valDiv = document.createElement('div');
    $(valDiv).addClass("alert alert-error");
    var innerV =  clientMessage.icon
                  + ' '
                  + '<strong>'
                  + clientMessage.header
                  + '</strong>';
    var messageBody = _populateMessageList(clientMessage.clientMessageDetails);
    $(valDiv).html(innerV + messageBody);
    $(validDiv).append(valDiv);
  }

  function _populateMessageList(clientMessageDetails) {
    var messageBody = '';
      messageBody += '<ul>';
      for (var i = 0; i < clientMessageDetails.length; i++) {
    	  if(clientMessageDetails[i].messageText.length>0)
    		  messageBody += '<li>' + clientMessageDetails[i].messageText + '</li>';
      }
      messageBody += '</ul>';
    
    return messageBody;
  }

  function _populateValidationMessages(clientInfo) {
    if (clientInfo &&clientInfo.showFieldValidation && clientInfo.hasItemValidationMessage) {
      // cleanUpOldMessages();
      // populate validation data on form/page
      var fieldNames = Object.keys(clientInfo.itemValidationClientMessages);
      for (var fieldNameI = 0; fieldNameI < fieldNames.length; fieldNameI++) {
        var fieldName = fieldNames[fieldNameI];
        var clientMessage = clientInfo.itemValidationClientMessages[fieldName];
        if ( !(fieldName === "none") ) {
          var myElement = $("#" + fieldName);
          if (myElement != null && (myElement.get(0) != null || myElement.get(0) != undefined)) {
            var myElementType = myElement.get(0).tagName.toUpperCase();
            switch(myElementType) {
              case('TABLE'):
                createTableValidationMessage(clientMessage, myElement);
                break;
              default:
                createInputValidationMessage(clientMessage, myElement);
                break;
            }
          }
        }
      }
      //scroll to the top of the modal
      if ($(document).find("div.in.modal div.modal-body")){
        $(document).find("div.in.modal div.modal-body").scrollTop("0");
      }
    }
  }

  function _createDidNotConvertDiv(message,fileNames) {
    var innerT = '<i class=\"fa fa-exclamation-triangle\"></i><strong>' + ' ' + message+'</strong>';
    var messageBody = '<UL>';
    for(var i=0;i<fileNames.length;i++) {
      messageBody+='<LI>'+fileNames[i]+'</LI>';
    }
    messageBody += '</UL>';

    var checkDiv = $('.conversionerror');

    if(checkDiv.length) {
      checkDiv.empty();
      checkDiv.html(innerT + messageBody );
    }
    else{
      checkDiv  = document.createElement('div');
      $(checkDiv).addClass("alert alert-error conversionerror");
      $(checkDiv).html(innerT + messageBody );
      $('#'+IParameterName.WEB_VALIDATION_DIV).append(checkDiv);
    }
  }

  function createTableValidationMessage(clientMessage, myElement) {
    for (var i = 0; i < clientMessage.clientMessageDetails.length; i++) {
      $(myElement).before("<div class=\"text-error\">" + clientMessage.clientMessageDetails[i].messageText + "</div>");
    }
    $(myElement).wrap( "<div class=\"ui-state-error\"></div>");
  }

  function createInputValidationMessage(clientMessage, myElement) {
    for (var i = 0; i < clientMessage.clientMessageDetails.length; i++) {
      var clientMessageDetail = clientMessage.clientMessageDetails[i];
      if (!$.isEmptyObject(myElement.get(0).type) && myElement.get(0).type.toUpperCase() == "FILE") {
        var errorDiv = $('#' + clientMessage.fieldName + '_ERROR');
        errorDiv.html(clientMessageDetail.messageText);
      } else {
        $(myElement).wrap( "<div class=\"control-group control-group-vertical error " + myElement.attr("id") + "_ERROR_DIV \"></div>");
        $(myElement).after("<div  id=\"controlErrorDiv\" class=\"text-error " + myElement.attr("id") + "_TEXT_ERROR_DIV\">" + clientMessageDetail.messageText + "</div>");
      }
    }
  }

  function _cleanUpOldValidations() {
    $(".text-error").empty();
    $(".control-group").removeClass("control-group control-group-vertical error");
    $(".ui-state-error").removeClass("ui-state-error");
  }

  function _cleanUpOldMessageDivs(notifDivId, validationDivId) {
    if (!notifDivId) {
      notifDivId = IParameterName.WEB_NOTIFICATION_DIV;
    }
    if (!validationDivId) {
      validationDivId = IParameterName.WEB_VALIDATION_DIV;
    }
    $("#" + notifDivId).empty();
    $("#" + validationDivId).empty();
    $("#" + IParameterName.WEB_MODAL_VALIDATION_DIV).empty();
  }

  return {
    populateValidationMessages : _populateValidationMessages,
    populateMessageList : _populateMessageList,
    cleanUpOldValidations : _cleanUpOldValidations,
    cleanUpOldMessageDivs : _cleanUpOldMessageDivs,
    populateNotifications : _populateNotifications,
    createDidNotConvertDiv : _createDidNotConvertDiv
  };
} )(jQuery);